package dao;

import java.io.Serializable;
import java.util.List;

import modelo.TipoDocumento;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

import util.HibernateUtil;

public class TipoDocumentoDao implements Serializable{

	
	private static final long serialVersionUID = 955526524264767014L;
	private Session session;
	SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
	
	public TipoDocumentoDao() {
		super();
	}

	public TipoDocumentoDao(Session session, SessionFactory sessionFactory) {
		super();
		this.session = session;
	}

	public List<TipoDocumento> obtenerTipoDocumento() {
		session=sessionFactory.openSession();
		
		Query query = session.createQuery("SELECT td FROM TipoDocumento td");
		List<TipoDocumento> td = query.list();
		
		session.close();
		
		return td;
	}
	
	public TipoDocumento obtenerTipoDocumentoById(int id){
		session=sessionFactory.openSession();
		Query query = session.createQuery("from TipoDocumento where IdTipoDocumento = :id ");
        query.setParameter("id", id);

        List<?> list = query.list();

        TipoDocumento td = (TipoDocumento)list.get(0);
        return td;
	}
	
	public void guardarTipoDocumento(TipoDocumento td){
		session=sessionFactory.openSession();
		session.beginTransaction();
		session.saveOrUpdate(td);
		session.getTransaction().commit();
		session.close();
	}

	public void eliminarTipoDocumento(TipoDocumento td) {
		session=sessionFactory.openSession();
		session.beginTransaction();
		session.delete(td);
		session.getTransaction().commit();
		session.close();
	}

	
	
	

}

